import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'

// 付款申请表单数据类型
interface PayFormData {
  payAmount: string
  payDate: string
  payObject: string
  payObjectText: string
  payRemark: string
  // 新增：附件字符串，保持与报销页面一致的参数名
  attachmentUrl: string
}

// 审批步骤数据类型
interface ApprovalStep {
  title: string
  subtitle?: string
  name: string
  avatar: string
  url: string
  id: string | number
}

export const usePayFormStore = defineStore('payForm', () => {
  // 表单数据
  const formData = reactive<PayFormData>({
    payAmount: '',
    payDate: '',
    payObject: '',
    payObjectText: '',
    payRemark: '',
    attachmentUrl: ''
  })

  // 审批步骤数据
  const approvalSteps = ref<ApprovalStep[]>([])

  // 提交状态
  const submitting = ref(false)

  // 选择器显示状态
  const showPayObjectSheet = ref(false)
  const showPayDatePicker = ref(false)

  // 日期选择器数组变量
  const payDateArray = ref<string[]>([])

  // 保存表单数据
  const saveFormData = (data: Partial<PayFormData>) => {
    Object.assign(formData, data)
  }

  // 更新单个字段
  const updateField = (field: keyof PayFormData, value: string) => {
    formData[field] = value
  }

  // 设置审批步骤
  const setApprovalSteps = (steps: ApprovalStep[]) => {
    approvalSteps.value = steps
  }

  // 设置提交状态
  const setSubmitting = (status: boolean) => {
    submitting.value = status
  }

  // 设置日期选择器数组
  const setPayDateArray = (dateArray: string[]) => {
    payDateArray.value = dateArray
  }

  // 重置表单数据
  const resetForm = () => {
    formData.payAmount = ''
    formData.payDate = ''
    formData.payObject = ''
    formData.payObjectText = ''
    formData.payRemark = ''
    formData.attachmentUrl = ''
    approvalSteps.value = []
    submitting.value = false
    showPayObjectSheet.value = false
    showPayDatePicker.value = false
    payDateArray.value = []
  }

  // 获取当前日期数组格式
  const getCurrentDateArray = () => {
    const now = new Date()
    const year = now.getFullYear().toString()
    const month = (now.getMonth() + 1).toString().padStart(2, '0')
    const day = now.getDate().toString().padStart(2, '0')
    return [year, month, day]
  }

  // 初始化日期数组
  const initializeDateArray = () => {
    payDateArray.value = getCurrentDateArray()
  }

  return {
    // 状态
    formData,
    approvalSteps,
    submitting,
    showPayObjectSheet,
    showPayDatePicker,
    payDateArray,
    
    // 方法
    saveFormData,
    updateField,
    setApprovalSteps,
    setSubmitting,
    setPayDateArray,
    resetForm,
    getCurrentDateArray,
    initializeDateArray
  }
})